package com.mobi.cacti.rabbit.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.mobi.cacti.model.ReqCactiData;
import com.mobi.cacti.model.ReqCactiDataHandler;
import com.mobi.cacti.model.ReqTFASTicket;
import com.mobi.cacti.model.ReqTFASTicketHandler;
import com.mobi.cacti.rabbit.response.ReqMobileLoginHandler;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;
import com.mobi.cacti.rabbit.response.ReqCustomerServiceInfo;
import com.mobi.cacti.rabbit.response.ReqCustomerServiceInfoHandler;
import com.mobi.cacti.rabbit.response.ReqRssItem;
import com.mobi.cacti.rabbit.response.ReqRssItemHandler;
import com.mobi.cacti.rabbit.response.ReqTicketCount;
import com.mobi.cacti.rabbit.response.ReqTicketCountHandler;
import com.mobi.cacti.rabbit.response.ReqTicketDetail;
import com.mobi.cacti.rabbit.response.ReqTicketDetailHandler;
import com.mobi.cacti.rabbit.response.ReqTicketHistory;
import com.mobi.cacti.rabbit.response.ReqTicketHistoryHandler;
import com.mobi.cacti.rabbit.response.ReqTicketRecent;
import com.mobi.cacti.rabbit.response.ReqTicketRecentHandler;

import android.util.Log;

//http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/
public class SAXXMLParser {
	
	public static ReqMobileLogin reqMobileLogin(InputStream is) {
		
		ReqMobileLogin data = null;
		try {
			ReqMobileLoginHandler saxHandler = new ReqMobileLoginHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();			
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}
		
		return data;
	}
	
	public static ReqCustomerServiceInfo respW4CusInfo(InputStream is) {
		ReqCustomerServiceInfo data = null;
		try {
			ReqCustomerServiceInfoHandler saxHandler = new ReqCustomerServiceInfoHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
		}

		return data;
	}
	
	public static ReqTicketCount respTicketCount(InputStream is) {
		ReqTicketCount data = null;
		try {
			ReqTicketCountHandler saxHandler = new ReqTicketCountHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}

		return data;
	}
	
	public static ArrayList<ReqTicketRecent> respTicketRecentList(InputStream is) {
		ArrayList<ReqTicketRecent> data = null;
		try {
			ReqTicketRecentHandler saxHandler = new ReqTicketRecentHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}

		return data;
	}
	
	public static ArrayList<ReqTicketHistory> respTicketHistoryList(InputStream is) {
		ArrayList<ReqTicketHistory> data = null;
		try {
			ReqTicketHistoryHandler saxHandler = new ReqTicketHistoryHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}

		return data;
	}
	
	public static ReqTicketDetail respTicketDetail(InputStream is) {
		ReqTicketDetail data = null;
		try {
			ReqTicketDetailHandler saxHandler = new ReqTicketDetailHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}

		return data;
	}
	
	public static ArrayList<ReqRssItem> respRssItem(InputStream is) {
		ArrayList<ReqRssItem> data = null;
		try {
			ReqRssItemHandler saxHandler = new ReqRssItemHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
			ex.printStackTrace();
		}

		return data;
	}
	
//---------------------------------------------------------------------------------------------
	
	
	public static ReqCactiData reqCactiData(InputStream is) {
		ReqCactiData data = null;
		try {
			ReqCactiDataHandler saxHandler = new ReqCactiDataHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();			
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
		}

		return data;
	}
	

	public static ReqTFASTicket reqTFASTicket(InputStream is) {
		ReqTFASTicket data = null;
		try {
			ReqTFASTicketHandler saxHandler = new ReqTFASTicketHandler();
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();			
			xmlReader.setContentHandler(saxHandler);
			xmlReader.parse(new InputSource(is));
			data = saxHandler.getObj();

		} catch (Exception ex) {
			Log.d("XML", "SAXXMLParser: parse() failed");
		}

		return data;
	}
	
	
	private static ReqMobileLogin parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
	{
		int eventType = parser.getEventType();
		ReqMobileLogin oClass = null;
		
		 while (eventType != XmlPullParser.END_DOCUMENT){
			 String name = null;
			 switch (eventType){
			 	case XmlPullParser.START_DOCUMENT:
			 		oClass = new ReqMobileLogin(); //create object class
                	break;
                case XmlPullParser.START_TAG:
                	name = parser.getName();
                	Log.d("tai", ">>"+name);
                	break;
                case XmlPullParser.END_TAG:
                	name = parser.getName();
                	Log.d("tai", ">"+name);
                	break;
             }
			 eventType = parser.next();
			 Log.d("tai", ">>>>");
		 }
		 
		 return oClass;
	}
	
}